package com.dz.goods.service;

import com.dz.common.base.BasePageResultsVO;
import com.dz.common.constants.CodeDomain;
import com.dz.goods.domain.entity.Brand;
import com.dz.goods.domain.entity.Category;
import com.dz.goods.domain.entity.QTemplate;
import com.dz.goods.domain.entity.Template;
import com.dz.goods.domain.repository.CategoryRepository;
import com.dz.goods.domain.repository.TemplateRepository;
import com.dz.goods.domain.vo.BrandResultsVO;
import com.dz.goods.domain.vo.TemplateParamsVO;
import com.dz.goods.domain.vo.TemplateResultsVO;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.QueryResults;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.lang3.StringUtils;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class TemplateServiceImpl implements TemplateService{

    @Autowired
    private TemplateRepository templateRepository;
    @Autowired
    private CategoryRepository categoryRepository;
    @Autowired
    private JPAQueryFactory jpaQueryFactory;
    @Autowired
    private ModelMapper modelMapper;
    @Transactional
    @Override
    public Template findById(Long id) {
        return templateRepository.findById(id).get();
    }
    @Transactional
    @Override
    public Template add(Template template) {
        return templateRepository.saveAndFlush(template);
    }
    @Transactional
    @Override
    public void delete(Long id) {
        Template template = templateRepository.findById(id).get();
        template.setDeleteFlag(Integer.valueOf(CodeDomain.DELETE_FLAG_YES.value()));
        templateRepository.saveAndFlush(template);
    }
    @Transactional
    @Override
    public Template edit(Template template) {
        return templateRepository.saveAndFlush(template);
    }
    @Transactional
    @Override
    public BasePageResultsVO<TemplateResultsVO> getTemplateList(TemplateParamsVO vo) {
        QTemplate template=QTemplate.template;
        BooleanBuilder where=new BooleanBuilder();
        if(StringUtils.isNotEmpty(vo.getId())){
            where.and(template.id.eq(Long.parseLong(vo.getId())));
        }
        if(StringUtils.isNotEmpty(vo.getName())){
            where.and(template.name.eq(vo.getName()));
        }
        if(StringUtils.isNotEmpty(vo.getParaNum())){
            where.and(template.paraNum.eq(Integer.valueOf(vo.getParaNum())));
        }
        if(StringUtils.isNotEmpty(vo.getSpecNum())){
            where.and(template.specNum.eq(Integer.valueOf(vo.getSpecNum())));
        }
        where.and(template.deleteFlag.eq(Integer.valueOf(CodeDomain.DELETE_FLAG_NO.value())));
        JPAQuery<Template> query=jpaQueryFactory.select(template).from(template).where(where.getValue());
        QueryResults<Template> fetchResults = query.offset((vo.getPageNum() - 1L) * vo.getPageSize()).limit(vo.getPageSize()).fetchResults();
        List<TemplateResultsVO> list=fetchResults.getResults().stream().map(e->TemplateResultsVO.builder()
                .id(e.getId())
                .name(e.getName())
                .paraNum(e.getParaNum())
                .specNum(e.getSpecNum()).build()).collect(Collectors.toList());
        return BasePageResultsVO.success(vo.getPageNum(),vo.getPageSize(),fetchResults.getTotal(),list);
    }
    @Transactional
    @Override
    public TemplateResultsVO findByCategoryId(Long id) {
        Category category = categoryRepository.findById(id).get();
        Template template = templateRepository.findById(category.getTemplateId()).get();
        TemplateResultsVO templateResultsVO = modelMapper.map(template, TemplateResultsVO.class);
        return templateResultsVO;
    }


}